package com.lin.mq.listener;

import com.lin.blog.mapper.ArticleCategoryTagsMapper;
import com.lin.blog.service.IElasticSearchService;
import com.lin.blog.vo.ArticleCategoryTagsVO;
import com.lin.constant.ResultCode;
import com.lin.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: blog
 * @description: 监听MQ保存文章消息
 * @author: Lin
 * @create: 2020-04-25 18:31
 **/
@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = "${rocketmq.topic_article_insert}",topic = "${rocketmq.topic_article_insert}")
public class SaveArticleListener implements RocketMQListener<Long> {

    private static Logger logger = LoggerFactory.getLogger(SaveArticleListener.class);

    @Autowired
    private IElasticSearchService elasticSearchService;

    @Autowired
    private ArticleCategoryTagsMapper articleCategoryTagsMapper;

    @Override
    public void onMessage(Long articleId) {
        logger.info("监听 {} 接收ID: {}",this.getClass().getSimpleName(),articleId);

        ArticleCategoryTagsVO articleCategoryTagsVO = articleCategoryTagsMapper.getArticleCategoryTagsVOById(articleId);

        Result result = elasticSearchService.saveArticleCategoryTagsVO(articleCategoryTagsVO);

        if (ResultCode.SUCCESS.getCode().equals(result.getCode())){
            logger.info("{} 处理成功", this.getClass().getSimpleName());
        }else{
            logger.info("{} 处理失败", this.getClass().getSimpleName());
        }
    }
}
